package com.example.dangerouscargodriver.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.baiju.netmanager.Http.IHttpRequest;
import com.baiju.style.citylist.Toast.ToastUtils;
import com.bumptech.glide.Glide;
import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.controller.AuthController;
import com.example.dangerouscargodriver.controller.EnterPlatformInfoController;
import com.example.dangerouscargodriver.controller.IControllerEventListener;
import com.example.dangerouscargodriver.entry.TruckDetailInfo;
import com.example.dangerouscargodriver.event.Event;
import com.example.dangerouscargodriver.event.LoadHttpResourceStatus;
import com.example.dangerouscargodriver.module.ResponseInfo;
import com.example.dangerouscargodriver.param.RemoteAPI;
import com.example.dangerouscargodriver.param.RemoteAPICmd;
import com.example.dangerouscargodriver.widget.ActionBar;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.HashMap;

public class PersonalTruckDetailInfoActivity extends HttpRequestActivity {

    private static final String TAG = "CarDetailInfoActivity";
    private static final int MSG_LOAD_ENTER_ATTR_DONE = 0;
    private ActionBar mActionBar = null;
    private MyControllerEventListener mControllerListener = null;
    private MyHandler mHandler = null;

    //
    private TruckDetailInfo mTruckDetailInfo = null;

    private TextView mCarNo = null;
    private TextView mCarType = null;
    private TextView mCarEnterStatus = null;
    private TextView mDeliverNum = null;
    private TextView mMainContact = null;
    private TextView mAssistContact = null;
    private ImageView mMainPic = null;
    private ImageView mCert6 = null;
    private ImageView mCert7 = null;
    private ImageView mCert8 = null;
    private ImageView mCert9 = null;

    private String mTruckId = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.initHttpComponent(PersonalTruckDetailInfoActivity.class);
        setContentView(R.layout.activity_personal_truck_detail_info);
        if(getIntent().getStringExtra("tid") != null){
            mTruckId = getIntent().getStringExtra("tid");
        }

        mControllerListener = new MyControllerEventListener();
        mHandler = new MyHandler(this);

        mCarNo = findViewById(R.id.tv_text_car_no);
        mCarType = findViewById(R.id.tv_text_car_type);
        mCarEnterStatus = findViewById(R.id.tv_text_car_status);
        mDeliverNum = findViewById(R.id.tv_text_deliver_num);
        mMainContact = findViewById(R.id.tv_text_main_contact);
        mAssistContact = findViewById(R.id.tv_text_assist_contact);
        mMainPic = findViewById(R.id.iv_car_main_pic);
        mCert6 = findViewById(R.id.iv_cert6);
        mCert7 = findViewById(R.id.iv_cert7);
        mCert8 = findViewById(R.id.iv_cert8);
        mCert9 = findViewById(R.id.iv_cert9);

        mActionBar = findViewById(R.id.actionBarCarDetailInfo);
        mActionBar.setOnBackClickListener( new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        EnterPlatformInfoController.Instance().attachControllerListener(
                PersonalTruckDetailInfoActivity.class,
                mControllerListener);
    }

    private void executeSearchPersonalInfo(){
        HashMap<String, String> header = new HashMap<String, String>();
        header.put("token", AuthController.Instance().getToken());

        if(mTruckId != null) {
            sendHttpRequest(
                    IHttpRequest.RequestMethod.GET_METHOD,
                    RemoteAPI.REQUEST_V1_USER_REGISTER_TRUCK + "/" + mTruckId,
                    header,
                    null,
                    RemoteAPICmd.REQUEST_V1_USER_REGISTER_TRUCK);
        }
        else{
            sendHttpRequest(
                    IHttpRequest.RequestMethod.GET_METHOD,
                    RemoteAPI.REQUEST_V1_USER_REGISTER_TRUCK + "/" +
                    EnterPlatformInfoController.Instance().getTid(),
                    header,
                    null,
                    RemoteAPICmd.REQUEST_V1_USER_REGISTER_TRUCK);
        }
    }

    private void refreshView(){
        if(mTruckDetailInfo != null){
            mCarNo.setText(mTruckDetailInfo.getTruckno());
            mCarType.setText(mTruckDetailInfo.getClassname());
            mCarEnterStatus.setText(mTruckDetailInfo.getAdultstatus_name());
            mDeliverNum.setText(mTruckDetailInfo.getTotal_trans());
            String main = mTruckDetailInfo.getContact() + " (" +
                    mTruckDetailInfo.getMobilephone() + ")";
            mMainContact.setText(main);

            if(mTruckDetailInfo.getContact_data().size() > 1) {
                String assist = mTruckDetailInfo.getContact_data().get(1).getContact() + " (" +
                        mTruckDetailInfo.getContact_data().get(1).getMobilephone() + ")";
                mAssistContact.setText(assist);
            }
            mMainPic.setBackgroundResource(0);
            Glide.with(getBaseContext()).load(mTruckDetailInfo.getMain_pic()).into(mMainPic);
            mCert6.setBackgroundResource(0);
            Glide.with(getBaseContext()).load(mTruckDetailInfo.getRoad_transport()).into(mCert6);
            mCert7.setBackgroundResource(0);
            Glide.with(getBaseContext()).load(mTruckDetailInfo.getSupercargo()).into(mCert7);
            mCert8.setBackgroundResource(0);
            Glide.with(getBaseContext()).load(mTruckDetailInfo.getQualification()).into(mCert8);
            mCert9.setBackgroundResource(0);
            Glide.with(getBaseContext()).load(mTruckDetailInfo.getDriving_permit()).into(mCert9);
        }
    }

    @Override
    protected void onHttpResponse(int actionId, String response) {
        try {
            JSONObject res = new JSONObject(response);
            Gson gson = new Gson();
            if(actionId == RemoteAPICmd.REQUEST_V1_USER_REGISTER_TRUCK){
                Log.d(TAG, "onResponse: " + response);
                dismissLoadingDialog();
                ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
                if(responseInfo != null && responseInfo.getStatus() == 1){
                    mTruckDetailInfo = gson.fromJson(res.getString("data"), TruckDetailInfo.class);
                    refreshView();
                }
            }
            else{

            }
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onHttpErrorResponse(int actionId, String response) {
        if(actionId == RemoteAPICmd.REQUEST_V1_USER_REGISTER_TRUCK){
            dismissLoadingDialog();
            ToastUtils.showLongToast(this, "获取车辆信息失败");
        }
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.d(TAG, "onDestroy");
        EnterPlatformInfoController.Instance().unAttachControllerListener(
                PersonalTruckDetailInfoActivity.class,
                mControllerListener);
    }

    private class MyHandler extends Handler {
        WeakReference<PersonalTruckDetailInfoActivity> mActivity;

        MyHandler(PersonalTruckDetailInfoActivity activity) {
            mActivity = new WeakReference<PersonalTruckDetailInfoActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            PersonalTruckDetailInfoActivity activity = mActivity.get();
            switch (msg.what){
                case MSG_LOAD_ENTER_ATTR_DONE:
                    activity.showLoadingDialog();
                    activity.executeSearchPersonalInfo();
                    break;
            }
        }
    }

    private class MyControllerEventListener implements IControllerEventListener {

        @Override
        public void onEvent(int eventId, String json) {
            if(eventId == Event.EVENT_LOAD_HTTP_ENTER_STATUS){
                Gson gson = new Gson();
                LoadHttpResourceStatus status = gson.fromJson(json, LoadHttpResourceStatus.class);
                Log.d(TAG, "onEvent: " + status.toJson());
                if(status.getWhat() == EnterPlatformInfoController.LOAD_ENTER_STATUS_DONE){
                    Message msg = new Message();
                    msg.what = MSG_LOAD_ENTER_ATTR_DONE;
                    mHandler.sendMessage(msg);
                }
            }
        }
    }
}